﻿using MCPlatform.UI.Controls.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MCPlatform.UI.Controls.Views
{
    /// <summary>
    /// TestTray.xaml 的交互逻辑
    /// </summary>
    public partial class TestTray : UserControl
    {
  
        private bool isMouseButtonDown;
        private Point startPoint;
        private Border? currentBoxSelectedBorder = null;

        public TestTray()
        {
            InitializeComponent();



        }


        #region properties

       




        public TestTrayModel TestTrayModel
        {
            get { return (TestTrayModel)GetValue(TestTrayModelProperty); }
            set { SetValue(TestTrayModelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TestTrayModel.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TestTrayModelProperty =
            DependencyProperty.Register("TestTrayModel", typeof(TestTrayModel), typeof(TestTray), new PropertyMetadata(default));




        #endregion

        #region Methods

        /// <summary>
        /// 鼠标左键按下（款选OK）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isMouseButtonDown = true;
            startPoint = e.GetPosition(canvas);
        }

        /// <summary>
        /// 鼠标左键抬起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (currentBoxSelectedBorder != null)
            {
                //获取选框的矩形位置
                Point tempEndPoint = e.GetPosition(this.canvas);
                Rect tempRect = new Rect(startPoint, tempEndPoint);
                //获取子控件
                List<Rectangle> childList = GetChildObjects<Rectangle>(this.canvas);
               
                foreach (var child in childList)
                {
                    
                    // 假设child是你的Rectangle控件
                    Rect childRect = new Rect(0, 0, child.Width, child.Height);

                    // 获取child在Canvas中的坐标
                    GeneralTransform transform = child.TransformToAncestor(canvas);
                    childRect.Location = transform.Transform(new Point(0, 0));

                    //若子控件与选框相交则改变样式
                    if (childRect.IntersectsWith(tempRect))
                    {
                        var clickedTrayItem = child.DataContext as TrayModel;
                        if (clickedTrayItem is null) { return; }
                        clickedTrayItem.Value = clickedTrayItem.Value == 1 ? 0 : 1;

                       


                    }

                }
                this.canvas.Children.Remove(currentBoxSelectedBorder);
                currentBoxSelectedBorder = null;
            }

            isMouseButtonDown = false;
        }

        private void canvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            isMouseButtonDown = true;
            startPoint = e.GetPosition(canvas);
        }

        private void canvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (currentBoxSelectedBorder != null)
            {
                //获取选框的矩形位置
                Point tempEndPoint = e.GetPosition(this.canvas);
                Rect tempRect = new Rect(startPoint, tempEndPoint);
                //获取子控件
                List<Rectangle> childList = GetChildObjects<Rectangle>(this.canvas);
                foreach (var child in childList)
                {
                    // 假设child是你的Rectangle控件
                    Rect childRect = new Rect(0, 0, child.Width, child.Height);

                    // 获取child在Canvas中的坐标
                    GeneralTransform transform = child.TransformToAncestor(canvas);
                    childRect.Location = transform.Transform(new Point(0, 0));

                    //若子控件与选框相交则改变样式
                    if (childRect.IntersectsWith(tempRect))
                    {                      
                        var clickedTrayItem = child.DataContext as TrayModel;
                        if (clickedTrayItem is null) { return; }
                        clickedTrayItem.Value = clickedTrayItem.Value == 2 ? 0 : 2;

                      


                    }

                }
                this.canvas.Children.Remove(currentBoxSelectedBorder);
                currentBoxSelectedBorder = null;
            }

            isMouseButtonDown = false;
        }
        private void canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseButtonDown)
            {
                Point tempEndPoint = e.GetPosition(this.canvas);
                //绘制跟随鼠标移动的方框
                DrawMultiselectBorder(tempEndPoint, startPoint);
            }
        }


        private void DrawMultiselectBorder(Point endPoint, Point startPoint)
        {
            if (currentBoxSelectedBorder == null)
            {
                currentBoxSelectedBorder = new Border();
                currentBoxSelectedBorder.Background = new SolidColorBrush(Colors.Orange);
                currentBoxSelectedBorder.Opacity = 0.4;
                currentBoxSelectedBorder.BorderThickness = new Thickness(2);
                currentBoxSelectedBorder.BorderBrush = new SolidColorBrush(Colors.OrangeRed);
                Canvas.SetZIndex(currentBoxSelectedBorder, 100);
                this.canvas.Children.Add(currentBoxSelectedBorder);
            }
            currentBoxSelectedBorder.Width = Math.Abs(endPoint.X - startPoint.X);
            currentBoxSelectedBorder.Height = Math.Abs(endPoint.Y - startPoint.Y);
            if (endPoint.X - startPoint.X >= 0)
                Canvas.SetLeft(currentBoxSelectedBorder, startPoint.X);
            else
                Canvas.SetLeft(currentBoxSelectedBorder, endPoint.X);
            if (endPoint.Y - startPoint.Y >= 0)
                Canvas.SetTop(currentBoxSelectedBorder, startPoint.Y);
            else
                Canvas.SetTop(currentBoxSelectedBorder, endPoint.Y);
        }

        public static List<T> GetChildObjects<T>(System.Windows.DependencyObject obj) where T : System.Windows.FrameworkElement
        {
            System.Windows.DependencyObject? child = null;
            List<T> childList = new List<T>();
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);
                if (child is T)
                {
                    childList.Add((T)child);
                }
                childList.AddRange(GetChildObjects<T>(child));
            }
            return childList;
        }






        #endregion


    }
}
